package com.cstz.hb.manage.controller.applet;

import com.cstz.enums.DeptTypeEnum;
import com.cstz.enums.DictTypeEnum;
import com.cstz.enums.QuestionStatusEnum;
import com.cstz.enums.UserTypeEnum;
import com.cstz.hb.manage.domain.AppUser;
import com.cstz.hb.manage.domain.SysDept;
import com.cstz.hb.manage.domain.SysQuestion;
import com.cstz.hb.manage.service.AppUserService;
import com.cstz.hb.manage.service.SysDeptService;
import com.cstz.hb.manage.service.SysQuestionService;
import com.cstz.ydshop.common.domain.DictDO;
import com.cstz.ydshop.common.service.DictService;
import com.cstz.ydshop.common.utils.*;
import com.cstz.ydshop.system.domain.DeptDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author bo.deng
 * @Project: xy_article
 * @Package com.cstz.hb.manage.controller.applet
 * @Description:
 * @date 2020/10/21  20:09
 */

@RestController
@RequestMapping("/applet/quest")
@Slf4j
public class AppletQuestController {

    @Autowired
    private SysQuestionService sysQuestionService;

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private DictService dictService;

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 获取全部问题类型
     * @return
     */
    @GetMapping("/getQuestTypes")
    public R getQuestTypes(){
        try{
           List<DictDO> dicts = dictService.listType(DictTypeEnum.问题类型.getIndex());
            return R.ok().put("data", dicts);
        }catch(Exception e){
            log.error("获取问题类型列表失败",e);
            return R.error("获取问题类型列表失败");
        }
    }

    /**
            * 获取协助单位
     * @return
             */
    @GetMapping("/getDepts")
    public R getDepts(){
        try{
            List<SysDept> sysDepts = sysDeptService.selectDepts(DeptTypeEnum.协助单位.getIndex());
            return R.ok().put("data", sysDepts);
        }catch(Exception e){
            log.error("获取协助单位失败",e);
            return R.error("获取协助单位失败");
        }
    }

    /**
     * 社区用户提交问题
     * @param sysQuestion
     * @return
     */
    @PostMapping("/addQuest")
    public R addQuest(@RequestBody SysQuestion sysQuestion){
        try{
            if(StringUtils.isBlank(sysQuestion.getContent()) || null == sysQuestion.getTypeId() ||
            null == sysQuestion.getUserId() || sysQuestion.getStatus()<=0){
                return R.error("参数不完整");
            }
            AppUser appUser = appUserService.get(sysQuestion.getUserId());
            if(null == appUser){
                return R.error("用户不存在");
            }
            if(UserTypeEnum.社区用户.getIndex() != appUser.getType()){
                return R.error("只有社区用户才能提交问题");
            }
            sysQuestion.setUserDeptId(appUser.getDeptId());
            if(StringUtils.isEmpty(sysQuestion.getFilluser())){
                sysQuestion.setFilluser(appUser.getCnName());
            }
            sysQuestionService.save(sysQuestion);
            return R.ok().put("data", sysQuestion.getId());
        }catch(Exception e){
            log.error("提交问题失败",e);
            return R.error("提交问题失败");
        }
    }

    /**
     * 分页获取问题列表
     * @param params
     * @return
     */
    @GetMapping("/getQuestionList")
    public PageUtils getQuestionList(@RequestParam Map<String, Object> params) throws Exception{
        if(null == params.get("userId")){
            throw new Exception("用户ID不能为空");
        }
        params.put("offset",(Integer.parseInt(params.get("offset").toString())-1)*Integer.parseInt(params.get("limit").toString()));
        Query query = new Query(params);
        List<SysQuestion> questions = sysQuestionService.selectQuestions(query);
        int total = sysQuestionService.count(query);
        PageUtils pageUtils = new PageUtils(questions, total);
        return pageUtils;
    }

    /**
     * 获取问题详情
     * @param params
     * @return
     */
    @GetMapping("/questDetail/{id}")
    public R questDetail(@PathVariable("id")Long id){
        try{
            SysQuestion sysQuestion = sysQuestionService.get(id);
            if(null == sysQuestion){
                return R.error("记录不存在");
            }
            sysQuestion.setStatusName(QuestionStatusEnum.getName(sysQuestion.getStatus()));
            return R.ok().put("data", sysQuestion);
        }catch(Exception e){
            log.error("获取问题详情失败",e);
            return R.error("获取问题详情失败");
        }
    }

}
